﻿(function ($) {
    $.fn.upload = function (options) {
        var settings = {
            dom: this,
            action: '',
            upload_type: '',
            params: {},
            onChooseFile: false,
            onSubmit: false
        };
        settings = $.extend(settings, options);
        var width = $(settings.dom).width();
        var height = $(settings.dom).height();
        var file = $('<input id="file0" name="file0" type="file"/>');
        file.attr({ accept: "image/jpeg,image/png", name: settings.upload_type });
        file.css({
            'position': 'absolute',
            width: '100%',
            height: '100%',
            left: 0,
            top: 0,
            right: 0,
            bottom: 0,
            opacity: 0
        });
        $(settings.dom).css({ 'position': 'relative', overflow: 'hidden' }).append(file);

        $("#file0").change(function () {
            if (this.files && this.files[0]) {
                var objUrl = getObjectURL(this.files[0]);
                if (objUrl) {
                    var formData = new FormData();
                    formData.append("file", $("#file0")[0].files[0]);
                    $.ajax({
                        url: "http://changyan.sohu.com/api/2/comment/attachment",
                        type: 'POST',
                        data: formData,
                        processData: false,
                        contentType: false,
                        beforeSend: function () {
                            layer.open({
                                content: '正在上传.....',
                                style: 'background:rgba(0,0,0, .6); color:#fff; border:none;',
                                time: 3,
                                shade: false
                            });
                        },
                        success: function (responseStr) {
                            var json = eval("(" + responseStr + ")");
                            $("#head_img").val(json.url);

                            $("#file_img01").attr("src", json.url);

                        },
                        error: function (responseStr) {
                            layer.open({
                                content: '错误，请重新上传.....',
                                style: 'background:rgba(0,0,0, .6); color:#fff; border:none;',
                                time: 2,
                                shade: false
                            });

                        }
                    });
                }
            } else {
                layer.open({
                    content: '图片错误.....',
                    style: 'background:rgba(0,0,0, .6); color:#fff; border:none;',
                    time: 3,
                    shade: false
                });

            }
        })
    }

    function getObjectURL(file) {
        var url = null;
        if (window.createObjectURL != undefined) {
            url = window.createObjectURL(file);
        } else if (window.URL != undefined) {
            url = window.URL.createObjectURL(file);
        } else if (window.webkitURL != undefined) {
            url = window.webkitURL.createObjectURL(file);
        }
        return url;
    }
})(jQuery)
$(function () {

    $("#btnSend").click(function () {
        var head_img = $('#head_img').val();
        if (head_img == '') {
            layer.open({
                content: '请上传你的头像...',
                style: 'background:rgba(0,0,0, .6); color:#fff; border:none;',
                time: 2,
                shade: false
            });
            return false;
        }

        var nickname = $("#nickname").val();
        if (nickname == '') {
            $('#nickname').focus();
            layer.open({
                content: '请填写您的昵称',
                style: 'background:rgba(0,0,0, .6); color:#fff; border:none;',
                time: 2,
                shade: false
            });
            return false;
        }
        var tel = $('#tel').val();
        if (tel == '') {
            $('#tel').focus();
            layer.open({
                content: '请输入您的电话号码',
                style: 'background:rgba(0,0,0, .6); color:#fff; border:none;',
                time: 2,
                shade: false
            });
            return false;
        }

        var wx_name = $('#wx_name').val();
        if (wx_name == '') {
            $('#wx_name').focus();
            layer.open({
                content: '请输入您的微信号',
                style: 'background:rgba(0,0,0, .6); color:#fff; border:none;',
                time: 2,
                shade: false
            });
            return false;
        }

        var AliName = $('#AliName').val();
        if (AliName == '') {
            $('#AliName').focus();
            layer.open({
                content: '请输入和支付宝绑定的真实姓名',
                style: 'background:rgba(0,0,0, .6); color:#fff; border:none;',
                time: 2,
                shade: false
            });
            return false;
        }
        var AlipayID = $('#AlipayID').val();
        if (AlipayID == '') {
            $('#AlipayID').focus();
            layer.open({
                content: '请输入您的支付宝账号',
                style: 'background:rgba(0,0,0, .6); color:#fff; border:none;',
                time: 2,
                shade: false
            });
            return false;
        }
        var data = $('form').serializeArray();
        $.post(
            '/u/userupdate.html',
            data,
            function (res) {
                layer.open({
                    content: res.Message,
                    style: 'background:rgba(0,0,0, .6); color:#fff; border:none;',
                    time: 2,
                    shade: false
                });
                setTimeout(" location.reload()", 2000);
            },
            'json'
        );
    })


})
function uploadAvatar() {
    $("#upload_avatar_btn").upload({
        action: 'http://changyan.sohu.com/api/2/comment/attachment',
        upload_type: 'avatar',
        onChooseFile: function (img) {
            $("#file_img01").attr('src', img.src);

        },
        onSubmit: function (resp) {

        }
    });

}
uploadAvatar();